propsage.namespace("modules.admin.modules.adsControl");

modules.admin.modules.adsControl.review = (function($, ui){
	var badWords = [];	
	var badWordsFound = [];
	
	function initComponents()
	{
		onCheckBoxChange();
		
		ui.doAjax('POST', baseUrl + '/admin/modules/adsControl/ajaxLoadFilters', {
			agency_id: agencyId
		}, onFiltersLoadSuccess);
	}

	
	function initEventHandlers() 
	{ 
		$('#Advertisement_approval_status input:radio').change(onCheckBoxChange)
	}
	
	function onCheckBoxChange()
	{
		var $radio_buttons = $('#Advertisement_approval_status input:radio');
		if($radio_buttons.filter(':checked').length > 0){
			$('#update-status').addClass('green').removeAttr('disabled');
		} else {
			$('#update-status').removeClass('green').attr("disabled", "disabled");
		}
	}
	
	
	
	function onFiltersLoadSuccess(response)
	{
		var totalScore = 0;
		var score = 0;
		
		badWords = response.data;
		
		var sentence = $("#Advertisement_ad_message").val();
		
		if(badWords){
			for (var j in badWords) {
				var phraseToTest = badWords[j].phrase.toLowerCase();
				var phraseLength = phraseToTest.length;
						
				var startIndex = sentence.toLowerCase().indexOf(phraseToTest);
						
				if (startIndex != -1) {
					var endIndex = startIndex + phraseLength;
					var subString = sentence.substring(startIndex, endIndex);
							
					score =  parseInt(badWords[j].score);
					
					var color = colorcode(score);
							
					var $span = $("<span>"+subString+"</span>");
					$span.addClass(color);
					var $html = $('<div/>').append($span).html();
								
					badWordsFound.push($html);
				}
			}
		}
		
		var unique = distinct(badWordsFound);
		$("#badWords").html(unique.join(' '))
	}
	
	function distinct(anArray)
	{
		var result = [];
		$.each(anArray, function(i,v){
			if ($.inArray(v, result) == -1) result.push(v);
		});
		return result;
	}
	
	function colorcode(score)
	{
		switch (true){
			case (score > 70):
				return "high"
				break;
			case (score > 30):
				return "medium"
				break; 
			default:
				return "low"
				break; 
		}
	}
	
	$(document).ready(function(){
		initComponents();
		initEventHandlers();
	});
	
}(jQuery, propsage.ui));